package com.chaoli.reggie_diy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaoli.reggie_diy.common.CustomException;
import com.chaoli.reggie_diy.common.R;
import com.chaoli.reggie_diy.domain.Orders;
import com.chaoli.reggie_diy.domain.Setmeal;
import com.chaoli.reggie_diy.domain.SetmealDish;
import com.chaoli.reggie_diy.dto.SetmealDto;
import com.chaoli.reggie_diy.mapper.SetmealMapper;
import com.chaoli.reggie_diy.service.OrderService;
import com.chaoli.reggie_diy.service.SetmealDishService;
import com.chaoli.reggie_diy.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐
     * 向套餐表中插入数据
     * 向套餐和菜品的关系表中插入数据
     *
     * @param setmealDto
     */
    @Override
    public void saveSetmealAndSetmealDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal表
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 给套餐菜品关系表中添加套餐id
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //保存菜品和套餐的关联信息，操作setmeal_dish表
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeSetmealAndDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus, 1);
        long count = this.count(setmealQueryWrapper);
        if (count > 0){
            //有套餐正在售卖，不能删除，抛出异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，删除setmeal中的数据
        this.removeBatchByIds(ids);
        //删除setmeal_dish中的数据
        //因为ids是setmeal的主键值，所以要通过条件查询去查SetmealId
        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        // delete from setmeal_dish where id in (1, 2, 3)
        setmealDishQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(setmealDishQueryWrapper);

    }

    /**
     * 根据ids批量启售和停售套餐
     * @param status
     * @param ids
     */
    @Override
    public void updateSetmealStatusByIds(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.in(ids != null, Setmeal::getId, ids);
        List<Setmeal> setmealList = this.list(setmealQueryWrapper);
        for (Setmeal setmeal : setmealList) {
            if (setmeal != null){
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }

    /**
     * 根据套餐id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealDto getData(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        //在setmeal_dish表中，根据套餐查询到菜品id，根据菜品id得到菜品的集合
        setmealDishQueryWrapper.eq(id != null, SetmealDish::getSetmealId, id);
        if (setmeal != null){
            BeanUtils.copyProperties(setmeal, setmealDto);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }else {
            throw new CustomException("该套餐获取异常");
        }
    }

    /**
     * 编辑套餐信息
     * @param setmealDto
     */
    @Override
    public R<String> edit(SetmealDto setmealDto) {
        if (setmealDto == null){
            return R.error("请求异常!!");
        }

        if (setmealDto.getSetmealDishes() == null){
            return R.error("套餐中没有菜品，请添加菜品");
        }

        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //删除setmeal_dish中有关setmealId的记录
        LambdaQueryWrapper<SetmealDish> setmealDishQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishQueryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(setmealDishQueryWrapper);

        //给setmeal_dish表中填充setmealId属性
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //更新数据
        setmealDishService.saveBatch(setmealDishes);
        this.updateById(setmealDto);

        return R.success("套餐修改成功");
    }

}
